#include "rm_meetingroom.h"
#include "ui_rm_meetingroom.h"
#include "mainwindow.h"
#include <QDebug>
#include <QTimer>


RM_MeetingRoom::RM_MeetingRoom(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RM_MeetingRoom)
{
    ui->setupUi(this);

    ui->widget_render->setParent(this);
    ui->widget_render->resize(200,200);
    ui->widget_render->move(-500,-500);

    m_userlist = new rm_userlist(this);
    m_userlist->hide();

#if defined(Q_OS_WIN32)
     av = GetFastPlay();
        DebugOut("ver: %0x\n", av->Ver());

        av->GetDevices()->SetCameraDef(720);
        // 枚举摄像头
        IDevices::LIST* list=new IDevices::LIST;
        list->type = IDevices::TYPE_CAMERA;
        HRESULT hr =av->GetDevices()->GetList(list);
        for (int i = 0;i < list->count;i++) {
            list->items[i].clear();
        }

        // 枚举麦克风
        list->type = IDevices::TYPE_MIC;
        hr = av->GetDevices()->GetList(list);

        //for (int i=0;i<list.count;i++ ) {
       // qDebug()<<"---------------------------"<<list.items[i].id;
       // }

        //hr = av->GetDevices()->SetDevice(IDevices::TYPE_MIC,list.items[0].id);

        // 枚举扬声器
        list->type = IDevices::TYPE_SPEAKER;
        hr = av->GetDevices()->GetList(list);

        // 创建推送器
        m_sender = av->GetSender();
        m_sender->SetPreview((HWND)ui->widget_render->winId());

#elif defined(Q_OS_LINUX)
        m_sender = create_avsender(ui->widget_video1->winId());
#endif


    listavplayer.append(ui->widget_video1);
    listavplayer.append(ui->widget_video2);
    listavplayer.append(ui->widget_video3);
    listavplayer.append(ui->widget_video4);
    listavplayer.append(ui->widget_video5);
    listavplayer.append(ui->widget_video6);
    listavplayer.append(ui->widget_video7);
    listavplayer.append(ui->widget_video8);
    listavplayer.append(ui->widget_video9);

    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video1->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video2->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video3->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video4->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video5->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video6->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video7->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video8->winId()));
    listavrenders.append(av->CreatePlayer((HWND)ui->widget_video9->winId()));


    for(int i=0;i<listavplayer.size();i++)
    {
        listavplayer.at(i)->installEventFilter(this);
    }
}

RM_MeetingRoom::~RM_MeetingRoom()
{
    delete ui;
}

void RM_MeetingRoom::on_toolButton_camera_clicked()
{

#if defined(Q_OS_WIN32)
    if(videoflag==IVideoSender::FlagVideo)
    {
        videoflag=0;
        ui->toolButton_camera->setStyleSheet("border-image: url(:/resouce/camera.png);");
        if(isscreen)
           m_sender->SetVideoMode(IVideoSender::VideoModeScreen);
        else
           m_sender->SetVideoMode(IVideoSender::VideoModeDisable);


    }
    else
    {
        videoflag=IVideoSender::FlagVideo;
        ui->toolButton_camera->setStyleSheet("border-image: url(:/resouce/uncamera.png);");

        if(isscreen)
           m_sender->SetVideoMode(IVideoSender::VideoModeScreenWithCamera);
        else
           m_sender->SetVideoMode(IVideoSender::VideoModeCamera);
    }
    //m_sender->SetFlags(audioflag | videoflag);
#elif defined(Q_OS_LINUX)

#endif
}

void RM_MeetingRoom::on_toolButton_desktop_clicked()
{
#if defined(Q_OS_WIN32)

    if(isscreen)
    {
        isscreen=false;
        ui->toolButton_desktop->setStyleSheet("border-image: url(:/resouce/desktop.png);");
        if(IVideoSender::FlagVideo == videoflag)
           m_sender->SetVideoMode(IVideoSender::VideoModeCamera);
        else
           m_sender->SetVideoMode(IVideoSender::VideoModeDisable);
    }
    else
    {
        isscreen=true;
        ui->toolButton_desktop->setStyleSheet("border-image: url(:/resouce/undesktop.png);");
        if(IVideoSender::FlagVideo == videoflag)
           m_sender->SetVideoMode(IVideoSender::VideoModeScreenWithCamera);
        else
           m_sender->SetVideoMode(IVideoSender::VideoModeScreen);
    }


#elif defined(Q_OS_LINUX)

#endif
}

void RM_MeetingRoom::on_toolButton_mic_clicked()
{

#if defined(Q_OS_WIN32)

    if(audioflag==IVideoSender::FlagAudio)
    {
        audioflag=0;
        ui->toolButton_mic->setStyleSheet("border-image: url(:/resouce/mic.png);");
    }
    else
    {
        audioflag=IVideoSender::FlagAudio;
        ui->toolButton_mic->setStyleSheet("border-image: url(:/resouce/unmic.png);");
    }
     m_sender->SetFlags(audioflag | IVideoSender::FlagVideo);

#elif defined(Q_OS_LINUX)

#endif
}

void RM_MeetingRoom::on_pushButton_user_clicked()
{
    if(m_userlist->isVisible())
    {
        m_userlist->hide();
    }
    else
    {
        m_userlist->resize(200,this->height());
        m_userlist->move(0,0);
        m_userlist->show();
    }
}

void RM_MeetingRoom::startPush(QString url)
{
    qDebug()<<"======================== startPush";
#if defined(Q_OS_WIN32) 
    m_sender->Start(url.toStdWString().c_str());
#elif defined(Q_OS_LINUX)
    m_sender->start(url.toStdString().c_str());
#endif

}
#include "../rmutils.h"

void RM_MeetingRoom::on_pushButton_exit_clicked()
{
    m_sender->SetPreview(NULL);
    m_sender->End();
    for (int i=0;i<listavrenders.size() ;i++ ) {
        #if defined(Q_OS_WIN32)
            listavrenders.at(i)->Load(NULL, IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoHigh);
            listavplayer.at(i)->update();
        #elif defined(Q_OS_LINUX)
                            mapuserplay[id] = create_avplayer(ui->widget_video2->winId());
                            QString url=QString("video://")+MainWindow::serverip+QString("/")+id;
                            qDebug()<<"Load Url:"<<url;
                            mapuserplay[id]->load(url.toStdString().c_str(),
                                if_avplayer::flag_audio | if_avplayer::flag_high_video);
        #endif
    }

    QJsonObject leaveobj;
    leaveobj.insert("type","Type_Leaveroom");
    leaveobj.insert("phone",MainWindow::userphone);
    leaveobj.insert("room",MainWindow::groomid);


    MainWindow::g_MainWin->m_webSocket.sendTextMessage(RMUtils::JsonToQstring(leaveobj));
    MainWindow::g_MainWin->leaveroom();
}

bool RM_MeetingRoom::eventFilter(QObject* obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonDblClick)
    {
        int index = listavplayer.indexOf((QWidget*)obj);
        if(MainWindow::g_MainWin->Tempuserlist.size()<=index)
            return QWidget::eventFilter(obj,event);

        if(bigmode)
            bigmode=false;
        else
            bigmode=true;

        for(int i=0;i<listavplayer.size();i++)
        {

            {
                if(listavplayer.at(i) == obj)
                {
                   listavplayer.at(i)->setVisible(true);

                   if(MainWindow::g_MainWin->Tempuserlist.size()>i)
                   {
                       if(MainWindow::g_MainWin->Tempuserlist.at(i)
                               == MainWindow::g_MainWin->userphone)
                       {
                           listavrenders.at(i)->SetFlag(IVideoPlayer::FlagVideoHigh);
                       }
                       else
                       {
                           listavrenders.at(i)->SetFlag(IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoHigh);
                       }
                   }

                }
                else
                {
                    listavplayer.at(i)->setVisible(!bigmode);
                    if(MainWindow::g_MainWin->Tempuserlist.size()>i)
                    {
                        if(MainWindow::g_MainWin->Tempuserlist.at(i)
                                == MainWindow::g_MainWin->userphone)
                        {
                            listavrenders.at(i)->SetFlag(IVideoPlayer::FlagVideoLow);
                        }
                        else
                        {
                            listavrenders.at(i)->SetFlag(IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoLow);
                        }
                    }
                    //listavrenders.at(i)->SetFlag(IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoLow);
                }
            }


        }
    }
    return QWidget::eventFilter(obj,event);
}

void RM_MeetingRoom::updateLoadVideos(QStringList users)
{
    //users.removeOne(MainWindow::userphone);
    for (int i=0;i<users.size() ;i++ ) {
        QString url=QString("video://")+MainWindow::serverip+QString("/")+users.at(i);
        qDebug()<<"Load Url:"<<url;
        #if defined(Q_OS_WIN32)
            if(MainWindow::userphone == users.at(i))
                listavrenders.at(i)->Load(url.toStdWString().c_str(),IVideoPlayer::FlagVideoLow);
            else
                listavrenders.at(i)->Load(url.toStdWString().c_str(), IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoLow);
        #elif defined(Q_OS_LINUX)
                            mapuserplay[id] = create_avplayer(ui->widget_video2->winId());
                            QString url=QString("video://")+MainWindow::serverip+QString("/")+id;
                            qDebug()<<"Load Url:"<<url;
                            mapuserplay[id]->load(url.toStdString().c_str(),
                                if_avplayer::flag_audio | if_avplayer::flag_high_video);
        #endif
    }

    for (int i=users.size();i<listavrenders.size() ;i++ ) {
        #if defined(Q_OS_WIN32)
            listavrenders.at(i)->Load(NULL, IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoHigh);
            listavplayer.at(i)->update();
        #elif defined(Q_OS_LINUX)
                            mapuserplay[id] = create_avplayer(ui->widget_video2->winId());
                            QString url=QString("video://")+MainWindow::serverip+QString("/")+id;
                            qDebug()<<"Load Url:"<<url;
                            mapuserplay[id]->load(url.toStdString().c_str(),
                                if_avplayer::flag_audio | if_avplayer::flag_high_video);
        #endif
    }

//    if(users.size()>9)
//        return;
//    qDebug()<<"users:"<<users;

//    QStringList rmovelist;
//    QMap<QString,IVideoPlayer*>::iterator ivplayers;
//    for (ivplayers=mapuserplay.begin();ivplayers!=mapuserplay.end();ivplayers++) {
//        if(!users.contains(ivplayers.key()))
//        {
//            rmovelist.push_back(ivplayers.key());

//            ivplayers.value()->Destroy();
//            mapplayrender[ivplayers.value()]->update();
//            listavplayer[mapplayrender[ivplayers.value()]]=false;
//            mapplayrender.remove(ivplayers.value());
//            ivplayers.value()=NULL;
//        }
//    }

//    foreach(QString id,rmovelist)
//    {
//        mapuserplay.remove(id);
//    }

//    for (int i=0;i<listavplayer.size() ;i++ ) {
//        if((users.size()-1)>=i)
//        {
//            QString id = users.at(i);
//            if(!mapuserplay.contains(id))
//            {
//                QMap<QWidget*,bool>::iterator manger;
//                for (manger=listavplayer.begin();manger!=listavplayer.end();manger++) {
//                    if(manger.value()==false)
//                    {
//    #if defined(Q_OS_WIN32)
//                        mapuserplay[id] = av->CreatePlayer((HWND)manger.key()->winId());
//                        mapplayrender[mapuserplay[id]]=manger.key();
//                        QString url=QString("video://")+MainWindow::serverip+QString("/")+id;
//                        qDebug()<<"Load Url:"<<url;
//                        mapuserplay[id]->Load(url.toStdWString().c_str(), IVideoPlayer::FlagAudio | IVideoPlayer::FlagVideoHigh);
//    #elif defined(Q_OS_LINUX)
//                        mapuserplay[id] = create_avplayer(ui->widget_video2->winId());
//                        QString url=QString("video://")+MainWindow::serverip+QString("/")+id;
//                        qDebug()<<"Load Url:"<<url;
//                        mapuserplay[id]->load(url.toStdString().c_str(),
//                            if_avplayer::flag_audio | if_avplayer::flag_high_video);
//    #endif
//                        break;
//                    }
//                }
//            }
//        }

//    }
}

